using Assets.Scripts.GDK;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace Brick
{
	public class SheXianH : gdk_base_UI
	{
		public TiggerBox trigger;

		private bool isRecive;

		private GameObject lineItem;

		private int playTimes;

		private List<Animator> lineItems;

		public override void OnLoad()
		{
			gdk_game_manager.AddEventListener(EventID.GAME_SHOOP_START, OnShotStart);
		}

		public override void UnLoad()
		{
			gdk_game_manager.RemoveEventListener(EventID.GAME_SHOOP_START, OnShotStart);
		}

		public override void OnStart()
		{
			lineItems = new List<Animator>();
		}

		public void OnRestart()
		{
			trigger.isTrigger = false;
			isRecive = false;
		}

		private void OnShotStart()
		{
			if (trigger.isTrigger && !isRecive)
			{
				trigger.isTrigger = false;
				isRecive = true;
				EnemyPool.GetInstance().RecycleObj(base.gameObject);
			}
		}

		private void OnTriggerEnter2D(Collider2D other)
		{
			if (!isRecive)
			{
				trigger.isTrigger = true;
				int col = GetComponent<EnemyArray>().getCol();
				int row = GetComponent<EnemyArray>().getRow();
				EnemyData enemyData = new EnemyData();
				enemyData.type = 1;
				enemyData.row = row;
				enemyData.col = col;
				gdk_game_manager.DispachEvent(EventID.GAME_DAOJU_SHEXIAN, enemyData);
				if (lineItems.Count == 1)
				{
					Animator ani = UnUsedToUsed();
					ani.gameObject.SetActive(value: true);
					Transform transform = ani.transform;
					Vector3 localPosition = base.transform.localPosition;
					transform.localPosition = new Vector3(0f - localPosition.x, 0f, 0f);
					ani.Play("tanqiu_zhixian", 0, 0f);
					StartCoroutine(YieldAniFinish(ani, "tanqiu_zhixian", delegate
					{
						ani.gameObject.SetActive(value: false);
						lineItems.Add(ani);
					}));
				}
				else
				{
					AddAnimator();
				}
			}
		}

		public GameObject AddOpenClose(string filename)
		{
			string id = "Prefabs/effect/" + filename;
			GameObject item = gdk_game_manager.loadPrefab(id);
			if ((bool)item)
			{
				item.transform.SetParent(base.transform);
				item.transform.localScale = new Vector3(1f, 1f, 1f);
				Transform transform = item.transform;
				Vector3 localPosition = base.transform.localPosition;
				transform.localPosition = new Vector3(0f - localPosition.x, 0f, 0f);
				Animator ani = item.GetComponent<Animator>();
				StartCoroutine(YieldAniFinish(ani, "tanqiu_zhixian", delegate
				{
					item.SetActive(value: false);
					lineItems.Add(ani);
				}));
			}
			return item;
		}

		public IEnumerator YieldAniFinish(Animator ani, string aniName, UnityAction action)
		{
			yield return null;
			AnimatorStateInfo stateinfo = ani.GetCurrentAnimatorStateInfo(0);
			if (stateinfo.IsName(aniName) && stateinfo.normalizedTime > 1f)
			{
				action();
			}
			else
			{
				StartCoroutine(YieldAniFinish(ani, aniName, action));
			}
		}

		public Animator UnUsedToUsed()
		{
			Animator result = lineItems[0];
			lineItems.RemoveAt(0);
			return result;
		}

		public Animator AddAnimator()
		{
			if (lineItems.Count == 1)
			{
				return UnUsedToUsed();
			}
			GameObject gameObject = AddOpenClose("zhixian_heng");
			Animator component = gameObject.GetComponent<Animator>();
			lineItems.Add(component);
			return component;
		}
	}
}

